package com.smartcommunity.service.waterEleGasUsage.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smartcommunity.base.common.CacheMap;
import com.smartcommunity.bean.entity.Village;
import com.smartcommunity.bean.model.Pager;
import com.smartcommunity.dao.waterEleGasUsage.WaterEleGasUsageMapper;
import com.smartcommunity.entity.village.OrganizationVillage;
import com.smartcommunity.entity.waterEleGasUsage.VillageWaterEleGasUsage;
import com.smartcommunity.entity.waterEleGasUsage.WaterEleGasUsage;
import com.smartcommunity.entity.waterEleGasUsage.req.HouseWaterEleGasUsageReq;
import com.smartcommunity.entity.waterEleGasUsage.req.HouseWaterEleGasUsageStatisticReq;
import com.smartcommunity.entity.waterEleGasUsage.req.VillageWaterEleGasPageReq;
import com.smartcommunity.entity.waterEleGasUsage.resp.HouseWaterEleGasUsageResp;
import com.smartcommunity.entity.waterEleGasUsage.resp.HouseWaterEleGasUsageStatisticResp;
import com.smartcommunity.entity.waterEleGasUsage.resp.VillageWaterEleGasPageResp;
import com.smartcommunity.service.waterEleGasUsage.WaterEleGasUsageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author nieming
 * @descrition
 * @date 2020/5/12
 */
@Slf4j
@Service
public class WaterEleGasUsageServiceImpl implements WaterEleGasUsageService {
    @Autowired
    private WaterEleGasUsageMapper waterEleGasUsageMapper;

    @Override
    public Pager<VillageWaterEleGasPageResp> queryVillageUsage(VillageWaterEleGasPageReq villageWaterEleGasPageReq) {
        Pager<VillageWaterEleGasPageResp> result = new Pager<VillageWaterEleGasPageResp>();
        villageWaterEleGasPageReq.setStart((villageWaterEleGasPageReq.getPageNo() - 1) * villageWaterEleGasPageReq.getSize());
        int count = waterEleGasUsageMapper.queryVillageUsageCount(villageWaterEleGasPageReq);
        if (count > 0) {
            List<VillageWaterEleGasPageResp> villageWaterEleGasPageResps = waterEleGasUsageMapper.queryVillageUsageList(villageWaterEleGasPageReq);
            if (!CollectionUtils.isEmpty(villageWaterEleGasPageResps)) {
                villageWaterEleGasPageResps = villageWaterEleGasPageResps.parallelStream().map(villageWaterEleGasPageResp -> {
                    Village village = CacheMap.VILLAGE_INFO_MAP.get(villageWaterEleGasPageResp.getVillageCode());
                    if (null != village) {
                        villageWaterEleGasPageResp.setVillageName(village.getVillageName());
                        villageWaterEleGasPageResp.setAddress(village.getAddress());
                        villageWaterEleGasPageResp.setPicUrl(village.getPicUrl());
                        OrganizationVillage organizationVillage = CacheMap.VILLAGE_ORG_MAP.get(village.getParentId());
                        if (null != organizationVillage) {
                            villageWaterEleGasPageResp.setOrgName(organizationVillage.getOrgName());
                        }
                    }
                    return villageWaterEleGasPageResp;
                }).collect(Collectors.toList());
            }
            result.setTotalRow(count);
            result.setList(villageWaterEleGasPageResps);
        } else {
            result.setTotalRow(0);
            result.setList(new ArrayList<VillageWaterEleGasPageResp>());
        }
        return result;
    }

    @Override
    public Pager<HouseWaterEleGasUsageResp> queryHouseUsage(HouseWaterEleGasUsageReq villageWaterEleGasPageReq) {
        int pageNumber = villageWaterEleGasPageReq.getPageNum();
        int pageSize = villageWaterEleGasPageReq.getPageSize();
        PageHelper.startPage(pageNumber, pageSize,villageWaterEleGasPageReq.getOrderField()+" "+villageWaterEleGasPageReq.getOrder());
        List<HouseWaterEleGasUsageResp> houseWaterEleGasUsageResps = waterEleGasUsageMapper.queryHouseUsage(villageWaterEleGasPageReq);
        int count = 0;
        if (houseWaterEleGasUsageResps != null) {
            count = (int) ((Page<HouseWaterEleGasUsageResp>) houseWaterEleGasUsageResps).getTotal();
        }
        Pager<HouseWaterEleGasUsageResp> realCompanyPeoplePager = new Pager<>(pageNumber, pageSize, count);
        realCompanyPeoplePager.setList(houseWaterEleGasUsageResps);
        return realCompanyPeoplePager;
    }

    @Override
    public List<VillageWaterEleGasUsage> statisticVillageUsage() {
        return waterEleGasUsageMapper.statisticVillageUsage();
    }

    @Override
    public List<HouseWaterEleGasUsageStatisticResp> houseUsageStatistic(HouseWaterEleGasUsageStatisticReq villageWaterEleGasPageReq) {
        //查询房屋该年水电气使用记录
//        criteria.andVillageCodeEqualTo(villageWaterEleGasPageReq.getVillageCode());
//        criteria.andBuildingNoEqualTo(villageWaterEleGasPageReq.getBuildingNo());
//        criteria.andHouseNoEqualTo(villageWaterEleGasPageReq.getHouseNo());
//        criteria.andUsageDateBetween(, );
        villageWaterEleGasPageReq.setStartDate(villageWaterEleGasPageReq.getYear() + "-01");
        villageWaterEleGasPageReq.setEndDate(villageWaterEleGasPageReq.getYear() + "-12");
        List<WaterEleGasUsage> waterEleGasUsages = waterEleGasUsageMapper.selectByCondition(villageWaterEleGasPageReq);
        //将列表转换为Map
        Map<String, WaterEleGasUsage> waterEleGasUsageMap = waterEleGasUsages.stream().collect(Collectors.toMap(WaterEleGasUsage::getUsageDate, a -> a, (k1, k2) -> k1));

        //获取当年的所有月份
        List<String> usageDates = getYearMonth(villageWaterEleGasPageReq.getYear());

        //封装返回结果
        List<HouseWaterEleGasUsageStatisticResp> result = new ArrayList<>();
        for (String usageDate : usageDates) {
            HouseWaterEleGasUsageStatisticResp temp = new HouseWaterEleGasUsageStatisticResp();
            temp.setUsageDate(usageDate);
            if (waterEleGasUsageMap.containsKey(usageDate)) {
                WaterEleGasUsage usage = waterEleGasUsageMap.get(usageDate);
                temp.setEleUsages(usage.getEleUsage());
                temp.setGasUsages(usage.getGasUsage());
                temp.setWaterUsages(usage.getWaterUsage());
            }
            result.add(temp);
        }
        return result;
    }

    public List<String> getYearMonth(String year) {
        List<String> years = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            if (i < 10) {
                years.add(year + "-0" + i);
            } else {
                years.add(year + "-" + i);
            }
        }
        return years;
    }
}
